﻿Imports Library

Public Class frmMain
    Private bIsinit As Boolean 'is initializing

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If g_webInterface.GetStockData() Then
            StockInfoLineControl1.SetStockInfo(g_webInterface.StockInfo)
            StockInfoChart1.SetCurrentStockInfo(g_webInterface.StockInfo)

           
        End If

    End Sub

    Private Sub frmMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If bIsinit Then Exit Sub

        'every time when the form is closing save current UIOption
        SaveUIOption(g_UIOption)

    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        bIsinit = True

        'initialize global variable
        g_webInterface = New Library.clsWebInterface("600429")
        g_UIOption = New clsUIOption
        g_UIOption = LoadUIOption()
        g_MyStocks = LoadMyStockFromFile()

        'initialize controls
        StockInfoChart1.SetMyStockData(g_MyStocks)
        StockInfoChart1.StockID = "600429"
        StockInfoChart1.DisplayProfit = g_UIOption.DisplayProfit
        StockInfoChart1.SetAlert(g_UIOption.Alert)

        StockInfoLineControl1.ShowList = g_UIOption.ShowList
        StockInfoLineControl1.IsScroll = g_UIOption.IsScroll


        'initialize context menu
        StockIDToolStripMenuItem.Checked = g_UIOption.ShowList(1)
        stockNameToolStripMenuItem.Checked = g_UIOption.ShowList(2)
        TodayOpenToolStripMenuItem.Checked = g_UIOption.ShowList(3)
        YesterdayCloseToolStripMenuItem.Checked = g_UIOption.ShowList(4)
        CurrentPriceToolStripMenuItem.Checked = g_UIOption.ShowList(5)
        TodayMaxToolStripMenuItem.Checked = g_UIOption.ShowList(6)
        TodayMinToolStripMenuItem.Checked = g_UIOption.ShowList(7)
        RefreshTimeToolStripMenuItem.Checked = g_UIOption.ShowList(8)

        DisplayProfitToolStripMenuItem.Checked = g_UIOption.DisplayProfit
        IsScrollToolStripMenuItem.Checked = g_UIOption.IsScroll

        bIsinit = False
        Timer1.Start()
    End Sub


    Private Sub EditCustomerStockToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EditCustomerStockToolStripMenuItem.Click
        frmSetMyStocks.ShowDialog()
        StockInfoChart1.SetMyStockData(g_MyStocks)
    End Sub


    Private Sub DisplayProfitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DisplayProfitToolStripMenuItem.Click
        g_UIOption.DisplayProfit = DisplayProfitToolStripMenuItem.Checked
        StockInfoChart1.DisplayProfit = DisplayProfitToolStripMenuItem.Checked
    End Sub

    Private Sub CMenuForChart_Opening(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles CMenuMain.Opening
        If TypeOf (CMenuMain.SourceControl) Is StockInfoChartControl Then
            DisplayProfitToolStripMenuItem.Visible = True
            DisplayFieldToolStripMenuItem.Visible = False
            IsScrollToolStripMenuItem.Visible = False
        End If

        If TypeOf (CMenuMain.SourceControl) Is StockInfoLineControl Then
            DisplayProfitToolStripMenuItem.Visible = False
            DisplayFieldToolStripMenuItem.Visible = True
            IsScrollToolStripMenuItem.Visible = True
        End If
    End Sub

    Private Sub StockLineControlToolStripMenuItem_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles YesterdayCloseToolStripMenuItem.CheckedChanged, TodayOpenToolStripMenuItem.CheckedChanged, TodayMinToolStripMenuItem.CheckedChanged, TodayMaxToolStripMenuItem.CheckedChanged, stockNameToolStripMenuItem.CheckedChanged, StockIDToolStripMenuItem.CheckedChanged, RefreshTimeToolStripMenuItem.CheckedChanged, CurrentPriceToolStripMenuItem.CheckedChanged
        If bIsinit Then Exit Sub

        g_UIOption.ShowList(1) = StockIDToolStripMenuItem.Checked
        g_UIOption.ShowList(2) = stockNameToolStripMenuItem.Checked
        g_UIOption.ShowList(3) = TodayOpenToolStripMenuItem.Checked
        g_UIOption.ShowList(4) = YesterdayCloseToolStripMenuItem.Checked
        g_UIOption.ShowList(5) = CurrentPriceToolStripMenuItem.Checked
        g_UIOption.ShowList(6) = TodayMaxToolStripMenuItem.Checked
        g_UIOption.ShowList(7) = TodayMinToolStripMenuItem.Checked
        g_UIOption.ShowList(8) = RefreshTimeToolStripMenuItem.Checked
        StockInfoLineControl1.ShowList = g_UIOption.ShowList

    End Sub


    Private Sub IsScrollToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles IsScrollToolStripMenuItem.Click
        g_UIOption.IsScroll = IsScrollToolStripMenuItem.Checked
        StockInfoLineControl1.IsScroll = IsScrollToolStripMenuItem.Checked
    End Sub

    Private Sub SetupToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SetupToolStripMenuItem.Click
        frmSettings.ShowDialog()
    End Sub

    Private Sub StockInfoChart1_Alert(ByVal Sender As System.Object) Handles StockInfoChart1.Alert
        Me.Text = Me.Text & "Alert"
    End Sub

End Class